package com.tensquare.base.service;

import com.tensquare.base.LabelDao;
import com.tensquare.base.pojo.Label;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.*;
import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IDEA
 * author:zhiXiang-Chen
 * Date:2019/2/26
 * Time:16:08
 */
@Service
public class LabelService {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;

    public List<Label> findAll(){
       return labelDao.findAll();
    }

    public void add(Label label) {

        long id = idWorker.nextId();
        label.setId(id+"");
        labelDao.save(label);
    }

    public Label findByid(String labelid) {
       return labelDao.findById(labelid).get();
    }

    public void update(Label label) {

        labelDao.save(label);
    }

    public void delete(String labelid) {
        labelDao.deleteById(labelid);
    }

    /**
     * 动态查询
     * @param searchMap
     * @return
     */
    public List<Label> search(Map searchMap) {

        Specification<Label> spec = createSpecification(searchMap);

        return labelDao.findAll(spec);
    }

    /**
     * 查询分页
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public Page<Label> search(Map searchMap, int page, int size) {
        Specification<Label> spec = createSpecification(searchMap);
        PageRequest pageRequest = new PageRequest(page - 1, size);

        return labelDao.findAll(spec,pageRequest);
    }

    /**
     * 创建spec的方法
     * @param searchMap
     * @return
     */
    private Specification<Label> createSpecification(Map searchMap) {
        return new Specification<Label>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //把所有的查询条件都放到集合里面去
                List<Predicate> predicateList = new ArrayList<Predicate>();
                //把前台传过来的参数,先判断是否为有,有就转换成查询查询条件,用jpa的动态查询
                if(searchMap.get("id") !=null &&  !"".equals(searchMap.get("id"))){
                    predicateList.add(cb.equal(root.get("id"),searchMap.get("id")));
                }
                if (searchMap.get("labelname")!=null && !"".equals(searchMap.get("labelname"))){
                    predicateList.add( cb.like( root.get("labelname").as(String.class),"%"+searchMap.get("labelname")+"%"));
                }
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                    predicateList.add(cb.equal(root.get("state"),searchMap.get("state")));
                }
                if(searchMap.get("recommend") !=null &&  !"".equals(searchMap.get("recommend"))) {
                    predicateList.add(cb.equal(root.get("recommend"), searchMap.get("recommend")));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }


}
